package com.stylefeng.guns.modular.system.controller;

import com.alipay.api.internal.util.StringUtils;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.system.model.ChannelSon;
import com.stylefeng.guns.modular.system.model.JarPackage;
import com.stylefeng.guns.modular.system.service.IChannelSonService;
import com.stylefeng.guns.modular.system.service.IJarPackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * 子渠道控制器
 *
 * @author fengshuonan
 * @Date 2018-10-25 15:01:45
 */
@Controller
@RequestMapping("/channelSon")
public class ChannelSonController extends BaseController {

    private String PREFIX = "/system/channelSon/";

    @Autowired
    private IChannelSonService channelSonService;

    @Autowired
    private IJarPackageService jarPackageService;

    /**
     * 跳转到子渠道首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "channelSon.html";
    }

    /**
     * 跳转到添加子渠道
     */
    @RequestMapping("/channelSon_add")
    public String channelSonAdd() {
        return PREFIX + "channelSon_add.html";
    }

    /**
     * 跳转到添加jar包里面
     */
    @RequestMapping("/channelSon_addJar/{channel}")
    public String channelSonAddJar(@PathVariable String channel, Model model) {
        List<JarPackage> jarPackagesList = jarPackageService.selectList(null);
        List<String> str = new ArrayList<String>();
        for (JarPackage jp : jarPackagesList) {
            str.add(jp.getJarName());
        }
        ChannelSonController.removeDuplicate(str);
        StringBuffer jarName = new StringBuffer("");
        for (String jarNameStr : str) {
            jarName.append(jarNameStr).append(",");
        }
        if (!StringUtils.isEmpty(jarName.toString())) {
            jarName.deleteCharAt(jarName.length() - 1);
        }
        model.addAttribute("jarName", jarName);
        model.addAttribute("channel", channel);
        model.addAttribute("item", jarPackagesList);
        return PREFIX + "channelSon_addJar.html";
    }

    /**
     * 获取jar包列表
     */
    @RequestMapping(value = "/jarList")
    @ResponseBody
    public Object jarList() {
        return jarPackageService.selectList(null);
    }

    /**
     * 跳转到修改子渠道
     */
    @RequestMapping("/channelSon_update/{channelSonId}")
    public String channelSonUpdate(@PathVariable Integer channelSonId, Model model) {
        ChannelSon channelSon = channelSonService.selectById(channelSonId);
        model.addAttribute("item", channelSon);
        LogObjectHolder.me().set(channelSon);
        return PREFIX + "channelSon_edit.html";
    }

    public static List removeDuplicate(List list) {
        HashSet h = new HashSet(list);
        list.clear();
        list.addAll(h);
        return list;
    }

    /**
     * 获取子渠道列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return channelSonService.selectList(null);
    }

    /**
     * 新增子渠道
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(ChannelSon channelSon) {
        channelSonService.insert(channelSon);
        return SUCCESS_TIP;
    }

    /**
     * 删除子渠道
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer channelSonId) {
        channelSonService.deleteById(channelSonId);
        return SUCCESS_TIP;
    }

    /**
     * 修改子渠道
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(ChannelSon channelSon) {
        channelSonService.updateById(channelSon);
        return SUCCESS_TIP;
    }

    /**
     * 子渠道详情
     */
    @RequestMapping(value = "/detail/{channelSonId}")
    @ResponseBody
    public Object detail(@PathVariable("channelSonId") Integer channelSonId) {
        return channelSonService.selectById(channelSonId);
    }
}
